/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import Modelo.Bono;
import Modelo.DAL.Bono_DAO;
import Acceso.Security;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import java.math.BigDecimal;

/**
 *
 * @author yngrdyn
 */
public class agregarTipoBonoAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        ActionErrors errors = new ActionErrors();
        agregarTipoBonoForm formBean = (agregarTipoBonoForm) form;
        String nombre = formBean.getNombre();
        String sede = formBean.getSede();
        String tipo = formBean.getTipo();
        String monto = formBean.getMonto();

        System.out.println(nombre + "," + sede + "," + "," + tipo + "," + monto);

        if (nombre.equals("")) {
            errors.add("nombre_falta", new ActionMessage("errors.nombre_falta"));
            System.out.println("error");
            saveErrors(request, errors);

            return mapping.findForward(FAILURE);
        }
        if (sede.equals("seleccionar")) {
            errors.add("sede_requerida", new ActionMessage("errors.sede_requerida"));
            saveErrors(request, errors);

            return mapping.findForward(FAILURE);
        }
        if (tipo.equals("seleccionar")) {
            errors.add("tipo_requerido", new ActionMessage("errors.tipo_requerido"));
            saveErrors(request, errors);

            return mapping.findForward(FAILURE);
        }
        if (monto.equals("seleccionar")) {
            errors.add("monto_requerido", new ActionMessage("errors.monto_requerido"));
            saveErrors(request, errors);

            return mapping.findForward(FAILURE);
        }

        System.out.println("nombre: " + nombre + "\nsede: " + sede + "\ntipo: " + tipo + "\nmonto: " + monto);
        Bono b = new Bono(nombre, tipo, sede, monto);

        Bono_DAO bono = new Bono_DAO();

        boolean verif = bono.insertarBono(b);
        if (!verif) {
            errors.add("bono_noAgregado", new ActionMessage("errors.bono_noAgregado"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }


        errors.add("bono_Agregado", new ActionMessage("errors.bono_Agregado"));
        saveErrors(request, errors);


        return mapping.findForward(SUCCESS);
    }
}
